package cn.bjit.controller;


import cn.bjit.enums.BizCodeEnum;
import cn.bjit.model.UserDO;
import cn.bjit.request.UserLoginRequest;
import cn.bjit.request.UserRegisterRequest;
import cn.bjit.service.FileService;
import cn.bjit.service.UserService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-05-31
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {
    @Autowired
private FileService fileService;
    @Resource
    private UserService userService;

    @ApiOperation("上传头像")
    @PostMapping("upload")
    public JsonData  uploadHeaderImg(@ApiParam(value = "长传文件",required = true)@RequestPart("file") MultipartFile file){
        String result =  fileService.uploadUserHeadImg(file);
        if(result!=null){
           return  JsonData.buildSuccess(result);
        }else{
           return   JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
        }
    }
    @ApiOperation("用户注册")
    @PostMapping("register")
    public JsonData register(@ApiParam("用户注册对象")@RequestBody UserRegisterRequest userRegisterRequest){

        return userService.register(userRegisterRequest);
    }
    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(@ApiParam("用户登录对象")@RequestBody UserLoginRequest userLoginRequest ){
        return userService.login(userLoginRequest);
    }

    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detail(){
        UserVO userVo=userService.findUserDetail();
        return JsonData.buildSuccess(userVo);
    }

}

